package cn.hzking.gjmall.activitys.second;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.mikhaellopez.circularimageview.CircularImageView;
import com.tencent.smtt.sdk.WebView;

import org.greenrobot.eventbus.EventBus;

import java.util.Locale;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.hzking.gjmall.R;
import cn.hzking.gjmall.activitys.MainActivity;
import cn.hzking.gjmall.base.ColorStatuBarActivity;
import cn.hzking.gjmall.bean.GoodDetailBean;
import cn.hzking.gjmall.net.RetrofitFactory;
import cn.hzking.gjmall.net.base.BaseObserver;
import cn.hzking.gjmall.utils.IconTextSpanUtils;
import cn.hzking.gjmall.utils.OnClickEvent;
import cn.hzking.gjmall.utils.helpers.GoodDetailConvenientBannerHelper;
import cn.hzking.gjmall.view.SimpleOptionView;
import cn.hzking.gjmall.view.convenientbanner.ConvenientBanner;
import cn.hzking.gjmall.view.countdown.RushBuyCountDownTimerView;

public class NYuanGoodDetailActivity extends ColorStatuBarActivity {

    @Bind(R.id.simpleTileView)
    SimpleOptionView simpleTileView;
    @Bind(R.id.nyuandetail_state)
    TextView nyuandetailState; //是否下架
    @Bind(R.id.nyuandetail_gohome)
    LinearLayout nyuandetailGohome; //返回主页
    @Bind(R.id.nyuandetail_kefu)
    LinearLayout nyuandetailKefu; //客服
    @Bind(R.id.unread_num)
    TextView unreadNum; //购物车数量
    @Bind(R.id.nyuandetail_gocar)
    RelativeLayout nyuandetailGocar;//购物车
    @Bind(R.id.nyuandetailnomalbuttom_addcar)
    TextView nyuandetailnomalbuttomAddcar; //加入购物车
    @Bind(R.id.nyuandetailnomalbuttom_buy)
    TextView nyuandetailnomalbuttomBuy; //立即购买
    @Bind(R.id.home_banner)
    ConvenientBanner homeBanner;//轮播图
    @Bind(R.id.nyuandetail_cuttime)
    RushBuyCountDownTimerView nyuandetailCuttime;//倒计时
    @Bind(R.id.nyuan_isziying)
    TextView nyuanIsziying; //是否自营
    @Bind(R.id.nyuandetail_title)
    TextView nyuandetailTitle; //商品标题
    @Bind(R.id.nyuandetail_newprice)
    TextView nyuandetailNewprice;//现在的价格
    @Bind(R.id.nyuandetail_oldprice)
    TextView nyuandetailOldprice;//原来的价格
    @Bind(R.id.nyuandetail_zhenqian)
    TextView nyuandetailZhenqian; //挣了多少钱
    @Bind(R.id.nyuandetail_suilv)
    TextView nyuandetailSuilv; //税率
    @Bind(R.id.nyuan_xuanchuan)
    TextView nyuanXuanchuan;  //n元选活动(可点击跳转)
    @Bind(R.id.nyuan_title_rl)
    RelativeLayout nYuanRl;//n元选活动(可点击跳转)
    @Bind(R.id.nyuandetail_text1)
    TextView nyuandetailText1;//发货
    @Bind(R.id.nyuandetail_text2)
    TextView nyuandetailText2;//包邮
    @Bind(R.id.nyuandetail_text3)
    TextView nyuandetailText3;//七天退换
    @Bind(R.id.home_bar_headimage)
    CircularImageView homeBarHeadimage;//店铺头像
    @Bind(R.id.nyuandetail_shopname)
    TextView nyuandetailShopname; //店铺名称
    @Bind(R.id.nyuandetail_shopdetaitext)
    TextView nyuandetailShopdetaitext; //店铺介绍
    @Bind(R.id.nyuandetail_shopdetail_ll)
    RelativeLayout nyuandetailShopdetailLl;
    @Bind(R.id.nyuandetail_goodliangdian_string)
    TextView nyuandetailGoodliangdianString;//产品亮点
    @Bind(R.id.nyuandetail_goodliangdian)
    LinearLayout nyuandetailGoodliangdian;//产品亮点布局
    @Bind(R.id.goodlist_webview)
    WebView goodlistWebview;//webview
    @Bind(R.id.detail_ll)
    LinearLayout detailLl;
    @Bind(R.id.nyuandetail_rl)
    RelativeLayout nyuandetailRl;
    private String itemId;
    private String imageUrl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_nyuan_good_detail);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        itemId = intent.getStringExtra("itemId");
        simpleTileView.setTitle("商品详情");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }

    //N元选商品详情内容
    private void showSeckillContent(GoodDetailBean goodDetailBean) {
        GoodDetailBean.DataBean data = goodDetailBean.getData();
        //轮播图
        String imageUrl = data.getImageUrl();
        JSONArray imageList = JSON.parseArray(imageUrl);
        new GoodDetailConvenientBannerHelper(this, homeBanner, imageList,false).initConvenientBanner();
        //标题
        String title = data.getTitle();//标题
        String sellPattern = data.getSellPattern();//商品类型
        IconTextSpanUtils.setText(this, title, sellPattern, nyuandetailTitle);
        //新价格
        nyuandetailNewprice.setText("¥ " + data.getPrice());
        //旧价格
        nyuandetailOldprice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); //中划线
        nyuandetailOldprice.setText("¥ " + data.getAvecostPrice());
        //赚的钱(不是商家,没有这个字段)
        if (data.getEarn() == null) {
            //todo 商品详情的赚钱,后续判断
            nyuandetailZhenqian.setVisibility(View.INVISIBLE);
        } else {
            nyuandetailZhenqian.setText("赚 ¥" + data.getEarn());
        }
        //跨境税率
        if (data.getTaxes() == null) {
            //todo 商品详情页跨境税率,后续判断
            nyuandetailSuilv.setVisibility(View.GONE);
        } else {
            nyuandetailSuilv.setText(String.format(Locale.CHINA, "跨境综合税率约%s", data.getTaxes()));
        }
        //商家提示语
        nyuandetailText1.setText(data.getDescribe());
        //包邮信息
        if (data.getIssuingBay() == 1) {//不免运费
            nyuandetailText2.setText("满98元包基础运费，超重超地区运费另计");

        } else {
            nyuandetailText2.setText("该商品免运费");
        }
        //是否七天包退
        if (data.isNonRefunded()) {
            nyuandetailText3.setText("该商品支持七天无理由退换货政策");
        } else {
            nyuandetailText2.setText("该商品不支持七天无理由退换货政策");
        }
        //是否有商品亮点介绍
        if (TextUtils.isEmpty(data.getSpecialFeature())) {
            nyuandetailGoodliangdian.setVisibility(View.GONE);

        } else {
            nyuandetailGoodliangdianString.setText(data.getSpecialFeature());
        }
        //加载html片段
        if (!TextUtils.isEmpty(data.getDesc())) {//webview加载html片段
            goodlistWebview.loadDataWithBaseURL(null, data.getDesc(), "text/html", "utf-8", null);

        }

        nyuandetailGohome.setOnClickListener(listener);//前往首页
        nyuandetailKefu.setOnClickListener(listener);//分享按钮
        nyuandetailnomalbuttomBuy.setOnClickListener(listener);//正常情况下立即购买
        nyuandetailnomalbuttomAddcar.setOnClickListener(listener);//加入购物车

    }

    private void getNetData(Activity activity, boolean isShow, boolean isCancel) {
        RetrofitFactory.getInstant().API()
                .getGoodDetailData(itemId)
                .compose(this.<GoodDetailBean>setThread())
                .subscribe(new BaseObserver<GoodDetailBean>(activity, isShow, isCancel) {
                    @Override
                    protected void onSuccees(GoodDetailBean goodDetailBean) throws Exception {
                        GoodDetailBean.DataBean data = goodDetailBean.getData();
                        boolean list = data.isList();//true表示正在上架,false表示已经下架
                        boolean soldOut = data.isSoldOut();//true表示已经售罄,false表示没有售罄
                        boolean whetherSnapup = data.isWhetherSnapup();//true表示是活动,false表示不是活动
                        boolean whetherVirtual = data.isWhetherVirtual();//true表示是虚拟商品,false表示不是虚拟商品
                        imageUrl = data.getImageUrl();
                        if (!whetherVirtual) {//正常商品详情
                            showSeckillContent(goodDetailBean);
                        }
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {

                    }
                });
    }

    OnClickEvent listener = new OnClickEvent() {
        @Override
        public void singleClick(View v) {
            switch (v.getId()) {
                case R.id.nyuandetail_gohome://返回首页
                    EventBus.getDefault().post("goHome");
                    Intent intent = new Intent(NYuanGoodDetailActivity.this, MainActivity.class);
                    startActivity(intent);
                    break;
                case R.id.nyuandetail_kefu://客服
                    break;
                case R.id.nyuandetail_gocar://购物车
                    break;
                case R.id.nyuandetailnomalbuttom_buy://购买
                    break;
                case R.id.nyuandetailnomalbuttom_addcar://加入购物车
                    break;
                case R.id.nyuan_title_rl://n元选跳转
                    break;
            }

        }
    };
}
